<?php
class AutenticacaoController extends Zend_Controller_Action
{
	public function indexAction()
	{
		 
	}

	public function loginAction()
	{
		 
		$frontController = Zend_Controller_Front::getInstance();
		$frontController->setParam('noViewRenderer', true);

		if($this->autenticacao($_POST)){
			
                        $session = Zend_Registry::get('session');
                    
                        if ($session->local == 'C')
                        {
                            echo '{"local":"Cliente"}';
                        }
                        if ($session->local == 'L')
                        {
                             echo '{"local":"Laboratorio"}'; 
                        }
                        
                        
                }
                else
                {	 
                            echo '{"local":"sair"}';
                }
	}

	public function autenticacao(array $post)
	{
		$authAdapter = Zend_Registry::get('authAdapter');

		$authAdapter->setTableName('usuario')
					->setIdentityColumn('login')
					->setCredentialColumn('senha');

		$authAdapter->setIdentity(strtolower($post['login']))
					->setCredential(md5($post['senha']));

		$result = $authAdapter->authenticate();

		if($result->isValid()){
			$info = $authAdapter->getResultRowObject(null, 'senha');

			$session = Zend_Registry::get('session');
			$session->login	= $info->login;
			$session->nomeUsuario  = $info->nome;
			$session->id    = $info->idUsuario;
			$session->local = $info->tipoUsuario;
                        $session->idCliente = $info->idCliente;
                       
			return true;
		}else{
			return false;
		}
	}

	public function logoutAction()
	{
		$session = Zend_Registry::get('session');
		unset($session->login);

		$this->_redirect('/autenticacao');
	}
	
        public function senhaAction()
        { 
            
        }
        public function senhaalterarAction()
        {
             $this->_helper->viewRenderer->setNoRender();
            
            
                    $login = $_POST['login'];
                    $senhaantiga = md5($_POST['senha']);
                    $nvaSenha1 = md5($_POST['senhaN']);
                    $nvaSenha2 = md5($_POST['senhaC']);
                    

                    $authAdapter = Zend_Registry::get('authAdapter');
                    $authAdapter->setTableName('usuario')
                                                ->setIdentityColumn('login')
                                                ->setCredentialColumn('senha');

                    $authAdapter->setIdentity(strtolower($login))
                                                ->setCredential($senhaantiga);

                    $result = $authAdapter->authenticate();

                    if($result->isValid()){ 
                    
                        $Usuario = $authAdapter->getResultRowObject(null, 'senha');
                        if(strcmp($nvaSenha1, $nvaSenha2) == 0)
                        {
                            
                            $daoUsr = new Usuario();
                            $result = $daoUsr->saveSenha($Usuario->idUsuario, $nvaSenha1);
                            print_r($result);
                            return true;
                        }
                        else
                        {
                            return false;
                        }

                    }
                    else
                    {
                        return false;
                    }

            
            
        }
}
?>